﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading;

namespace Ession
{
    /// <summary>
    /// 单个下载器
    /// </summary>
    class DownloaderThread
    {
        public DownloaderThread()
        {
            InitWebClient();

        }
        public DownloaderThread(Uri uri, string data)
        {
            InitWebClient();
            this.uri = uri;
            this.Data = data;
        }
        public string Data;
        public bool downloading = false;
        public string foundPwdStr;
        public string OKString = "not Set";
        public string Result;
        public Thread thread;
        public Uri uri;
        private int oKLength;
        private WebClient wc;//= new WebClient();
        public int OkLength
        {
            get { return oKLength; }
            set { oKLength = value; }
        }
        public event EventHandler foundPwd;

        public void AddCookies(string cookies)
        {
            wc.Headers.Add(HttpRequestHeader.Cookie, cookies);
        }
        public string GetHTML(Uri uri)
        {
            downloading = true;
            string result = wc.DownloadString(uri.OriginalString);
            downloading = false;
            return result;
        }
        public string GetHTML(Uri uri, string cookies)
        {
            downloading = true;
            wc.Headers.Add(HttpRequestHeader.Cookie, cookies);
            return GetHTML(uri);
        }

        public void InvokeFoundPwd(EventArgs e)
        {
            EventHandler handler = foundPwd;

            if (handler != null) handler(this, e);
        }
        public string PostData(Uri uri, string data)
        {
            //   finished = false;
            InitWebClient();
            if (wc.Headers[HttpRequestHeader.ContentType] == null)
            {
                wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");

            }
            string result = wc.UploadString(uri.OriginalString, data);
            //    finished = true;
            return result;
        }
        public void Start()
        {
            thread = new Thread(ThreadExecute);
            thread.Start();
        }

        private void InitWebClient()
        {
            wc = new WebClient();

            wc.Headers = new WebHeaderCollection();

            wc.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)");
            wc.Headers.Add(HttpRequestHeader.Accept, "*/*");
        }
        void ThreadExecute()
        {
            try
            {
                downloading = true;
                Result = PostData(uri, Data);
                if (Result.Contains(OKString))
                {
                    foundPwdStr = Data;
                    InvokeFoundPwd(EventArgs.Empty);
                }
                Thread.Sleep(2000);
                downloading = false;
            }
            catch (Exception ex)
            {
                Thread.Sleep(5000);
                try
                {
                    Result = PostData(uri, Data);
                    if (Result.Contains(OKString))
                    {
                        foundPwdStr = Data;
                        InvokeFoundPwd(EventArgs.Empty);
                    }
                }
                catch (Exception)
                {

                }
                finally
                {
                    downloading = false;

                }

            }

        }
    }
}
